package edu.hebeu.activity;

import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import edu.hebeu.R;
import edu.hebeu.fragment.main.CarFragment;
import edu.hebeu.fragment.main.HomeFragment;
import edu.hebeu.fragment.main.MeFragment;
import edu.hebeu.fragment.main.RecommendFragment;

public class HomeActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private long exitTime = 0; // 保存点击了"首次"退出按钮的时间

    private List<Fragment> fragments;
    private int lastFragment; // 指定当前的Fragment

    private HomeFragment homeFragment = new HomeFragment();
    private CarFragment carFragment = new CarFragment();
    private MeFragment meFragment = new MeFragment();
    private RecommendFragment recommendFragment = new RecommendFragment();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initViews() {
        // 获取ActionBar
        ActionBar actionBar = getSupportActionBar();
        // 设置ActionBar的标题隐藏
        actionBar.setDisplayShowTitleEnabled(false);

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.mainBottomNav);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        fragments = new ArrayList<>();
        fragments.add(homeFragment);
        fragments.add(carFragment);
        fragments.add(meFragment);
        fragments.add(recommendFragment);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fgm, fragments.get(0))
                .commit();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        menuItem.setChecked(true);
        switch (menuItem.getItemId()) {
            case R.id.home:
                switchFragment(0); // 切换到第0个Fragment
                break;
            case R.id.car:
                switchFragment(1); // 切换到第2个Fragment
                break;
            case R.id.me:
                switchFragment(2); // 切换到第3个Fragment
                break;
            case R.id.recommend:
                switchFragment(3); // 切换到第1个Fragment
                break;
        }
        return false;
    }

    // 第一步、重写onKeyDown()方法来拦截用户单击物理按键的事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) { // 如果是返回键
            exit();
            return true; // 返回true，表示将该事件消费掉
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 这个方法用来实现切换Fragment，从 lastFragment 切换到 target
     * @param target
     */
    private void switchFragment(int target) {
        if (lastFragment == target) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragments.get(target).isAdded()) { // 如果要切换到的Fragment是添加过的
            transaction.show(fragments.get(target)); // 将要切换到的Fragment显示处理
        } else { // 否则，即要切换的Fragment是未添加过的
            transaction.add(R.id.fgm, fragments.get(target)); // 将要切换到的Fragment加入
        }
        transaction.hide(fragments.get(lastFragment)) // 将上一个(切换到Fragment之前的哪个Fragment)隐藏
                .commit();
        lastFragment = target; // 将当前的Fragment位置更新为要切换到的Fragment的位置
    }

    // 第二步、编写退出方法
    public void exit() {
        // 判断两次按键时间差是否大于2秒
        if(System.currentTimeMillis() - exitTime > 2000) {
            Toast.makeText(HomeActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis(); // 保存此次按键的时间
        } else {
            System.exit(0); // 退出
        }
    }

}
